<template>
  <div>
    <!-- 店铺图片上传 -->
    <el-upload
      ref="upload"
      :http-request="upload"
      :before-upload="beforeAvatarUpload"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :limit="1"
      :file-list="fileList"
      :on-success="handleSuccess"
      class="upload-demo fl"
      action="''"
      accept>
      <el-button size="mini" type="primary" round>点击上传</el-button>
    </el-upload>
    <!-- <div class="fl">
      <el-button type="primary" @click="submitUpload" size="mini" round style="margin: 70px 0 0 10px;">提 交</el-button>
    </div> -->
    <div class="clear"/>
    <el-dialog :visible.sync="dialogVisible">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import {
  createId
} from '@/utils/index' // 一个生产唯一的id的方法
import OSS from 'ali-oss'
// oss 上传
export default {
  name: 'ImgUpload',
  data() {
    return {
      uploadParams: {
        uploadPath: '',
        uploadSize: ''
      },
      fileList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      imageUrl: '',
      filePath: '',
      ossData:{
        region: 'oss-cn-shanghai',
        endpoint: 'http://oss-cn-shanghai.aliyuncs.com',
        accessKeyId: 'LTAI5tMevYwnihXXHmuSvNiZ',
        accessKeySecret: 'P9mYHb9ddkTZuPPW8DP7LnaIiF3VZS',
        bucket: 'daocuiaib2b'
      }
    }
  },
  created() {
    
  },
  methods: {
    // 图片上传前验证
    beforeAvatarUpload(file) {
      // file.size / 1024 图片的大小
      const format = /^image\/(jpeg|png|gif|bmp|jpg)$/.test(file.type);
      const isLt1M = file.size / 1024 / 1024 < 2
      this.uploadParams.uploadSize = file.size
      if (!format) {
        this.$message.error('上传图片只能是 JPEG, BMP, JPG，PNG，GIF格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return format && isLt1M
    },
    // 上传图片到OSS 同时派发一个事件给父组件监听
    upload(item) {
      let type
      const client = new OSS({
        region: this.ossData.region,
        accessKeyId: this.ossData.accessKeyId,
        accessKeySecret: this.ossData.accessKeySecret,
        bucket: this.ossData.bucket
      })
      switch (item.file.type) {
        case 'image/jpeg':
          {
            type = 'jpeg'
          }
          break
        case 'image/jpg':
          {
            type = 'jpg'
          }
          break
        case 'image/gif':
          {
            type = 'gif'
          }
          break
        case 'image/png':
          {
            type = 'png'
          }
          break
      }
      const key = 'BCenter/time=' + new Date().getTime() + '&id=' + this.guidGenerator() + '.' + type // 存储路径，并且给图片改成唯一名字
      return client.put(key, item.file).then(res => { // OSS上传
        const url = res.url
        this.uploadParams.uploadPath = client.signatureUrl(key, {
          expires: 999999999999999
        }) // 可访问的链接
        this.$refs.upload.submit()
      })
    },
    guidGenerator() {
      var S4 = function() {
        return ((1 + Math.random()) * 0X10000 | 0).toString(16).substring(1)
      }
      return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
    },
    handleSuccess(res, file) {
      this.filePath = URL.createObjectURL(file.raw)
      this.fileList = [{name: this.filePath, url: this.uploadParams.uploadPath}]
      console.log(this.filePath, this.uploadParams.uploadPath,"上传路径");
      this.$emit('changeSrc', this.filePath, this.uploadParams.uploadPath)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleRemove(file, fileList) {

    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
  .upload-demo {
    display: inline-block;
    line-height: 32px;
    margin-left: 10px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #2c9b69;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 60px;
    height: 60px;
    line-height: 60px;
    text-align: center;
  }

  .avatar {
    width: 140px;
    height: 140px;
    display: block;
  }

  .el-upload-list--picture-card .el-upload-list__item {
    width: 100px;
    height: 100px;
  }

  .el-upload--picture-card {
    width: 100px;
    height: 100px;
    line-height: 102px;
  }
</style>
